/*
 * The copyrights of this file belong to purple flower.

 * purple flower All rights reserved.
 *
 * No part of this document may be reproduced or transmitted in any form or by
 * any means, electronic or mechanical, including photocopying and recording,
 * for any purpose without prior written permission of purple flower
 *
 * This software contains a module which is based on the work of OpenIntents.
 * OpenIntents is published under "Apache License 2", the same license under
 * which Android is released. This will provide commercial and non-commercial 
 * third-party developers the same options and possibilities as Android itself.
 * If you want to use those codes, please connect to me (purpleflower912@gmail.com)
 */
package com.spk.quake2;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceGroup;
import android.view.KeyEvent;

import com.spk.quake2.R;

public class SettingActivity extends PreferenceActivity
		implements Preference.OnPreferenceChangeListener {

	public static final String[] preferenceKeys = {
		"controller_up",
		"controller_down",
		"controller_left",
		"controller_right",
		"controller_fire",
		"controller_tab",
		"controller_escape",		
		"controller_space",
		"controller_backspace",
		"controller_alt",
		"controller_shift",	
		"controller_f1",
		"controller_f2",
		"controller_f3",
		"controller_f4",	
		"controller_0",
		"controller_1",
		"controller_2",		
		"controller_3",		
		"controller_4",		
		"controller_5",	
		"controller_6",
		"controller_7",
		"controller_8",		
		"controller_9",	
		"controller_a",		
		"controller_b",	
		"controller_c",	
		"controller_d",	
		"controller_e",	
		"controller_f",	
		"controller_g",	
		"controller_h",		
		"controller_i",
		"controller_j",		
		"controller_k",	
		"controller_l",
		"controller_m",
		"controller_n",
		"controller_o",
		"controller_p",
		"controller_q",
		"controller_r",
		"controller_s",		
		"controller_t",
		"controller_u",	
		"controller_v",
		"controller_w",	
		"controller_x",
		"controller_y",
		"controller_z",
		"controller_dot",
		"controller_plus",
		"controller_minus",
		"controller_slash",
	};

	private static final int[] keyDisplayNames = {
		R.string.controller_up,
		R.string.controller_down,
		R.string.controller_left,
		R.string.controller_right,

		R.string.controller_fire,
		R.string.controller_tab,		
		R.string.controller_escape,		
		R.string.controller_space,
		R.string.controller_backspace,
		R.string.controller_alt,
		R.string.controller_shift,	
		R.string.controller_f1,	
		R.string.controller_f2,
		R.string.controller_f3,
		R.string.controller_f4,	
		R.string.controller_0,
		R.string.controller_1,
		R.string.controller_2,		
		R.string.controller_3,		
		R.string.controller_4,		
		R.string.controller_5,	
		R.string.controller_6,
		R.string.controller_7,
		R.string.controller_8,		
		R.string.controller_9,	
		R.string.controller_a,		
		R.string.controller_b,	
		R.string.controller_c,	
		R.string.controller_d,	
		R.string.controller_e,	
		R.string.controller_f,	
		R.string.controller_g,	
		R.string.controller_h,		
		R.string.controller_i,
		R.string.controller_j,		
		R.string.controller_k,	
		R.string.controller_l,
		R.string.controller_m,
		R.string.controller_n,
		R.string.controller_o,
		R.string.controller_p,
		R.string.controller_q,
		R.string.controller_r,
		R.string.controller_s,		
		R.string.controller_t,
		R.string.controller_u,	
		R.string.controller_v,
		R.string.controller_w,	
		R.string.controller_x,
		R.string.controller_y,
		R.string.controller_z,
		R.string.controller_dot,
		R.string.controller_plus,
		R.string.controller_minus,
		R.string.controller_slash,
	};

	private static final int defaultKeys_standard[] = {
		KeyEvent.KEYCODE_DPAD_UP,
		KeyEvent.KEYCODE_DPAD_DOWN,
		KeyEvent.KEYCODE_DPAD_LEFT,
		KeyEvent.KEYCODE_DPAD_RIGHT,
		KeyEvent.KEYCODE_ENTER,
		KeyEvent.KEYCODE_TAB,
		KeyEvent.KEYCODE_DEL,
		KeyEvent.KEYCODE_SPACE,
		KeyEvent.KEYCODE_CLEAR,
		KeyEvent.KEYCODE_ALT_LEFT,
		KeyEvent.KEYCODE_SHIFT_LEFT,
		0,
		0,
		0,
		0,
		KeyEvent.KEYCODE_0,
		KeyEvent.KEYCODE_1,
		KeyEvent.KEYCODE_2,
		KeyEvent.KEYCODE_3,
		KeyEvent.KEYCODE_4,
		KeyEvent.KEYCODE_5,
		KeyEvent.KEYCODE_6,
		KeyEvent.KEYCODE_7,
		KeyEvent.KEYCODE_8,
		KeyEvent.KEYCODE_9,
		KeyEvent.KEYCODE_A,
		KeyEvent.KEYCODE_B,
		KeyEvent.KEYCODE_C,
		KeyEvent.KEYCODE_D,
		KeyEvent.KEYCODE_E,
		KeyEvent.KEYCODE_F,
		KeyEvent.KEYCODE_G,	
		KeyEvent.KEYCODE_H,
		KeyEvent.KEYCODE_I,
		KeyEvent.KEYCODE_J,
		KeyEvent.KEYCODE_K,
		KeyEvent.KEYCODE_L,
		KeyEvent.KEYCODE_M,
		KeyEvent.KEYCODE_N,
		KeyEvent.KEYCODE_O,
		KeyEvent.KEYCODE_P,
		KeyEvent.KEYCODE_Q,
		KeyEvent.KEYCODE_R,
		KeyEvent.KEYCODE_S,
		KeyEvent.KEYCODE_T,		
		KeyEvent.KEYCODE_U,
		KeyEvent.KEYCODE_V,
		KeyEvent.KEYCODE_W,
		KeyEvent.KEYCODE_X,
		KeyEvent.KEYCODE_Y,
		KeyEvent.KEYCODE_Z,
		KeyEvent.KEYCODE_SYM,
		KeyEvent.KEYCODE_PLUS,	
		KeyEvent.KEYCODE_MINUS,
		KeyEvent.KEYCODE_SLASH,
	};

	private static final int defaultKeys_non_standard[] = {
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		KeyEvent.KEYCODE_VOLUME_UP,
		0,
		0,
		KeyEvent.KEYCODE_VOLUME_DOWN,
		KeyEvent.KEYCODE_SEARCH,
		KeyEvent.KEYCODE_BACK,
		0,
		0,
		KeyEvent.KEYCODE_CALL,
		KeyEvent.KEYCODE_CAMERA,
		0,
		0,		
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,	
	};

	private static boolean isKeyboardStandard(Context context) {
		return (context.getResources().getConfiguration().keyboard ==
				Configuration.KEYBOARD_QWERTY);
	}

	public static int[] getDefaultKeys(Context context) {
		return (isKeyboardStandard(context) ?
				defaultKeys_standard : defaultKeys_non_standard);
	}

	public static boolean getDefaultVirtualKeypadEnabled(Context context) {
		return !isKeyboardStandard(context);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTitle(R.string.settings_title);
		addPreferencesFromResource(R.xml.settings);

		PreferenceGroup group =
				(PreferenceGroup) findPreference("gameKeyBindings");

		int[] defaultKeys = getDefaultKeys(this);
		for (int i = 0; i < preferenceKeys.length; i++) {
			KeySetting pref = new KeySetting(this);
			pref.setKey(preferenceKeys[i]);
			pref.setTitle(keyDisplayNames[i]);
			pref.setDefaultValue(defaultKeys[i]);
			group.addPreference(pref);
		}
	}

	public boolean onPreferenceChange(Preference preference, Object newValue) {

		return true;
	}
}
